﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Classified.Data.Code;
using Classified.Service;
using Quartz;
using Quartz.Impl;

namespace Classified.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.Add("DomainRoute", new DomainRoute(
            //    "about.KRBClassified.com",                           // Domain with parameters
            //    "/{action}/{id}",                               // URL with parameters
            //    new { controller = "Home", action = "About", id = "" }  // Parameter defaults
            //));

          //  routes.MapRoute(
          //"ClassifiedDetails", "classifiedAds/{id}/{seName}/",
          //new { controller = "classifiedAds", action = "Details", id = UrlParameter.Optional, seName = UrlParameter.Optional },
          //new[] { "Classified.Web.Controllers" });
            #region Image Resizer Routes
            routes.MapRoute(
               "RenderImage", "Images/{file}",
               new { controller = "Images", action = "Render", file = "" }
           );

            routes.MapRoute(
                "RenderImageWithResizeAndWatermark", "Images/{width}/{height}/w/{file}",
                new { controller = "Images", action = "RenderWithResizeAndWatermark", width = "", height = "", file = "" }
            );

            routes.MapRoute(
                "RenderImageWithResize", "Images/{width}/{height}/{folder}/{file}",
                new { controller = "Images", action = "RenderWithResize", width = "", height = "", file = "" }
            );
            routes.MapRoute(
                "RenderImageWithResizeProfile", "Images/{width}/{height}/{file}",
                new { controller = "Images", action = "RenderWithResizeProfile", width = "", height = "", file = "" }
            );
            #endregion

            routes.MapRoute(
       "EmailVerificationlink", "myadsindex",
       new { controller = "Account", action = "myadsindex", UN = UrlParameter.Optional },
       new[] { "Classified.Web.Controllers" });
            routes.MapRoute(
      "EmailVerified-link", "EmailVerified",
      new { controller = "classifiedAds", action = "EmailVerified", UN = UrlParameter.Optional },
      new[] { "Classified.Web.Controllers" });

            routes.MapRoute(
         "ClassifiedReply", "classified/reply/{id}",
         new { controller = "classifiedAds", action = "ReplyAds", id = UrlParameter.Optional},
         new[] { "Classified.Web.Controllers" });

            routes.MapRoute(
          "ClassifiedSearch", "search-result/{categoryId}/{TextValue}",
          new { controller = "Home", action = "SearchResult", categoryId = UrlParameter.Optional, TextValue = UrlParameter.Optional },
          new[] { "Classified.Web.Controllers" });

             routes.MapRoute(
           "GetClassifiedAdsLocations", "classifiedAds/GetClassifiedAdsLocations/{id}/",
           new { controller = "classifiedAds", action = "GetClassifiedAdsLocations", id = UrlParameter.Optional, seName = UrlParameter.Optional },
           new[] { "Classified.Web.Controllers" });

             routes.MapRoute(
            "HomeClassifiedAds", "classified/{id}/{seName}",
            new { controller = "classifiedAds", action = "ClassifiedList", id = UrlParameter.Optional, seName = UrlParameter.Optional },
            new[] { "Classified.Web.Controllers" }
        );

             routes.MapRoute(
             "Pages", "page/{name}",
             new { controller = "Home", action = "Page", name = UrlParameter.Optional },
             new[] { "Classified.Web.Controllers" }
             );


            routes.MapRoute(
                "About-Us",                           // Domain with parameters
                "about-us",                               // URL with parameters
                new { controller = "Home", action = "About", id = "" }  // Parameter defaults
            );
           
            routes.MapRoute(
              "Default", // Route name
              "{controller}/{action}/{id}", // URL with parameters
              new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = "" },
               new[] { "Classified.Web.Controllers" }
                // Parameter defaults
          );

        }

        public static void ConfigureQuartzJobs()
        {
            // construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler sched = schedFact.GetScheduler();
            sched.Start();

            JobDetail activityEmailSenderService = new JobDetail("activityEmailSenderService",
                                                                 typeof(EmailService));
            //Activity email sender service
            SimpleTrigger triggeractivityEmailSenderService = new SimpleTrigger("triggerActivityEmailSenderService",
                                                                                null,
                                                                                DateTime.UtcNow,
                                                                                null,
                                                                                SimpleTrigger.RepeatIndefinitely,
                                                                                TimeSpan.FromMinutes(5));
            triggeractivityEmailSenderService.JobName = activityEmailSenderService.Name;
            triggeractivityEmailSenderService.Priority = 10;
            sched.ScheduleJob(activityEmailSenderService, triggeractivityEmailSenderService);
        }
        public static void CheckUrl()
        {
            
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            ConfigureQuartzJobs();
        }
    }
}